package com.wmh.baseservice.common.wechat.mp.template;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import com.alibaba.fastjson.JSONObject;
import com.wmh.baseservice.common.utils.CommUtil;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 订阅消息工具类
 */
@Component
public class WxSubMsgUtils {

    @Resource
    private WxMpService wxMpService;

    public static WxMpService staticWxMpService;

    @PostConstruct
    public void init() {
        staticWxMpService = wxMpService;
    }

    /**
     * 发货通知
     *
     * @param openId 接收方openId
     */
    public static boolean delivery(String openId, String title, String goodsTitle, String orderNum, String courierCompany,
                                   String courierNumber, String address, String remark) {
        WxMpTemplateData data1 = new WxMpTemplateData("first", title, "#c4261d");
        WxMpTemplateData data2 = new WxMpTemplateData("keyword1", goodsTitle, "#177dd4");
        WxMpTemplateData data3 = new WxMpTemplateData("keyword2", orderNum, "#177dd4");
        WxMpTemplateData data4 = new WxMpTemplateData("keyword3", courierCompany, "#177dd4");
        WxMpTemplateData data5 = new WxMpTemplateData("keyword4", courierNumber, "#177dd4");
        WxMpTemplateData data6 = new WxMpTemplateData("keyword5", address, "#177dd4");
        WxMpTemplateData data7 = new WxMpTemplateData("remark", remark, "#177dd4");
        return sendTemplate(openId, Arrays.asList(data1, data2, data3, data4, data5, data6, data7), "merchant/merchantsDetail",
                WxTemplateMsgConstants.TEMP_NOTICE_USER_DELIVERY);
    }

    /**
     * 新订单通知
     *
     * @param openId 接收方openId
     */
    public static boolean newOrder(String openId, String title, Date payTime, String orderType, String nick,
                                   String orderNum, String remark) {
        SimpleDateFormat format = new SimpleDateFormat("MM月dd日 HH时mm分");
        WxMpTemplateData data1 = new WxMpTemplateData("first", title, "#c4261d");
        WxMpTemplateData data2 = new WxMpTemplateData("tradeDateTime", format.format(payTime), "#177dd4");
        WxMpTemplateData data3 = new WxMpTemplateData("orderType", orderType, "#177dd4");
        WxMpTemplateData data4 = new WxMpTemplateData("customerInfo", nick, "#177dd4");
        WxMpTemplateData data5 = new WxMpTemplateData("orderItemName", "订单编号");
        WxMpTemplateData data6 = new WxMpTemplateData("orderItemData", orderNum, "#177dd4");
        WxMpTemplateData data7 = new WxMpTemplateData("remark", remark, "#177dd4");
        return sendTemplate(openId, Arrays.asList(data1, data2, data3, data4, data5, data6, data7),
                "团购订单".equals(orderType) ? "merchant/groupManage/orderList" : "merchant/landManage/order/list",
                WxTemplateMsgConstants.TEMP_NOTICE_MERCHANT_ORDER);
    }

    /**
     * 新聊天通知
     *
     * @param openId 接收方openId
     */
    public static boolean newMessage(String openId) {
        WxMpTemplateData data1 = new WxMpTemplateData("first", "您有一条新的聊天消息，请进入小程序查看", "#c4261d");
        WxMpTemplateData data3 = new WxMpTemplateData("keyword1", "聊天会话", "#177dd4");
        WxMpTemplateData data4 = new WxMpTemplateData("keyword2", "小小田野", "#177dd4");
        WxMpTemplateData data5 = new WxMpTemplateData("remark", "新消息通知");
        return sendTemplate(openId, Arrays.asList(data1, data3, data4, data5),
                "pages/growVegetables/growVegetables", WxTemplateMsgConstants.TEMP_NEW_MESSAGE);
    }


    public static boolean sendTemplate(String openId, List<WxMpTemplateData> wxMpTemplateDataArrayList, String url, String templateId) {
        WxMpTemplateMessage wxMpTemplateMessage = WxMpTemplateMessage.builder()
                .templateId(templateId).toUser(openId)
                .data(wxMpTemplateDataArrayList).miniProgram(new WxMpTemplateMessage.MiniProgram("wx703c3613347ddfc0", url, false)).build();

        try {
            staticWxMpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);
        } catch (WxErrorException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
